package com.kangye.exam.service.serviceimpl;

import com.kangye.exam.bean.Itembank;
import com.kangye.exam.dao.ItembankDao;
import com.kangye.exam.service.intellectTestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(value = "eTtestService")
public class intellectTestServiceImpl implements intellectTestService {

    @Resource
    private ItembankDao itembankDao;


    @Override
    public Map<String, Object> creatTest(Integer subjectId, Integer simple, Integer multip, Integer judge, Integer difficulty,Integer gradeId) {
        //dao层传值时的map
        Map<String, Object> map = new HashMap<>();
        //返回的map
        Map<String, Object> map1 = new HashMap<>();
        //dao层返回的所有题用list接收
        List<Itembank> list = new ArrayList<>();
        //单选题
        map.put("gradeId",gradeId);
        map.put("typeId",1);
        map.put("limit",simple);
        map.put("subjectId",subjectId);
        list = itembankDao.creatTest(map);
        map1.put("simple",list);
        //多选题
        map.put("typeId",2);
        map.put("limit",multip);
        list = itembankDao.creatTest(map);
        map1.put("multip",list);
        //判断题
        map.put("typeId",3);
        map.put("limit",judge);
        list = itembankDao.creatTest(map);
        map1.put("judge",list);
        map1.put("subjectId",subjectId);
        return map1;
    }


    @Override
    public boolean wrongItem(Integer stuId, String error, String errorId) {
        boolean flag = true;
        Map<String,Object> map = new HashMap<>();
        map.put("studentId",stuId);
        String[] ids = errorId.split(",");
        int j = 0;//题目Id索引
        //单选题判断题循环放入数据库
        String[] simpleAndJudge = error.substring(0,error.indexOf("/")).split(",");
        for(int i = 0;i<simpleAndJudge.length;i++){
            map.put("itemId",Integer.parseInt(ids[j++]));
            map.put("errorAnswer",simpleAndJudge[i]);
            map.put("errorTime",datetime());
            if(itembankDao.wrongItem(map)<=0){
                flag = false;
            }
        }
        //多选题错题循环放入数据库
        String[] multip = error.substring(error.indexOf("/")).split("/");
        for(int i = 0;i<multip.length;i++){
            if(!multip[i].equals("")){
                map.put("itemId",Integer.parseInt(ids[j++]));
                map.put("errorAnswer",multip[i]);
                map.put("errorTime",datetime());
                if(itembankDao.wrongItem(map)<=0){
                    flag = false;
                }
            }
        }
        return flag;
    }


    public static String datetime(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return simpleDateFormat.format(date);
    }
}
